package datos.clases;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import datos.interfaces.ITarjetaDAO;
import tipos.clases.Tarjeta;

public class JDBCTarjetaDAO implements ITarjetaDAO{

public List select() {
		
		Connection c = ConnectionManager.getInstance().checkOut();
		List res = new LinkedList();
		PreparedStatement stmt = null;
        ResultSet result = null;        
        String sql = "SELECT * FROM Tarjeta";
        
        try{
        	if(c==null){
        		System.out.println("No hay conexion");
        	}
        	stmt = c.prepareStatement(sql);      
        	stmt.executeQuery(sql);
            result = stmt.executeQuery();    
            while (result.next()) {
                Tarjeta temp = new Tarjeta();               
                temp.setPropietario(result.getString("Propietario"));
                temp.setTipo(result.getString("Tipo"));     
                temp.setNumero(result.getString("Numero"));    
                temp.setCVV(result.getInt("Cvv"));
                temp.setCaducidad(result.getString("Caducidad"));
                res.add(temp);
            }
            
        }catch(SQLException e){
        	System.out.println("Error");
        }finally{
        	  try {
                  if (result != null) {
                      result.close();
                  }
                  if (stmt != null) {
                      stmt.close();
                  }
              } catch (SQLException e) {
              }
        }
		return res;
	}

public void inserta(Tarjeta t) {
	
	Connection c = ConnectionManager.getInstance().checkOut();		
	PreparedStatement stmt = null;
        
    String sql = "INSERT INTO Tarjeta(Propietario,Tipo,Numero,Cvv,Caducidad) VALUES (?,?,?,?,?)";	
    
    try {
    	
        stmt = c.prepareStatement(sql);
        stmt.setString(1, t.getPropietario());
        stmt.setString(2, t.getTipo());
        stmt.setString(3, t.getNumero());
        stmt.setInt(4, t.getCVV());
        stmt.setString(5,t.getCaducidad());        
        
        stmt.executeUpdate();

    } catch (SQLException e) {
        System.out.println("Message: " + e.getMessage());
        System.out.println("SQLState: " + e.getSQLState());
        System.out.println("ErrorCode: " + e.getErrorCode());
    } finally {
        try {
            if (stmt != null) {
                stmt.close();
            }
        } catch (SQLException e) {
        }
    }
    
}

public void elimina(String t) {
	
	Connection c = ConnectionManager.getInstance().checkOut();		
	PreparedStatement stmt = null;
        
    String sql = "DELETE From Tarjeta Where Numero = ?";	
    
    try {    	
        stmt = c.prepareStatement(sql);
        stmt.setString(1,t);        
        stmt.executeUpdate();

    } catch (SQLException e) {
        System.out.println("Message: " + e.getMessage());
        System.out.println("SQLState: " + e.getSQLState());
        System.out.println("ErrorCode: " + e.getErrorCode());
    } finally {
        try {
            if (stmt != null) {
                stmt.close();
            }
        } catch (SQLException e) {
        }
    }
    
}
	
}
